package com.app.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileManager {

    private static final String EXT_STORAGE_PATH_PREEFIX = "CommandP";

    private static Bitmap getBitmapFromPath(String photoPath){
        if (!new File(photoPath).exists()) {
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeFile(photoPath, options);
    }

    public static Bitmap getBitmapFromPath(String photoPath, int sampleSize)
    {
        if (!new File(photoPath).exists()) {
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = sampleSize;
        return BitmapFactory.decodeFile(photoPath, options);
    }

    public static File getCommandPDirAllApiLevels() {
        File dir =null;
        if(isExternalStorageWritable()){
            dir = new File(Environment.getExternalStorageDirectory(),EXT_STORAGE_PATH_PREEFIX);
            if(!dir.exists()){
                dir.mkdirs();
                try {
                    new File(Environment.getExternalStorageDirectory() + "/" + EXT_STORAGE_PATH_PREEFIX + "/.nomedia").createNewFile();
                } catch (IOException e) {
                    return  null;
                }
            }
        }
        return  dir;
    }

    public static Bitmap getLayerPhoto(String photoPath){
        return getBitmapFromPath(photoPath);
    }

    public static BitmapFactory.Options getOut(String photoName){
        String fileName = "IMG_" + photoName + "_cover.png";
        String photoPath = getCommandPDirAllApiLevels().getAbsolutePath() + "/" + fileName;
        BitmapFactory.Options options = new BitmapFactory.Options();
        //加载图像的尺寸而不是图像本身
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);
        return options;
    }

    public static BitmapFactory.Options getOutFull(String photoPath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);
        return options;
    }

    public static Bitmap getWorkPhoto(String photoPath){
        return getBitmapFromPath(photoPath);
    }



    private static boolean isExternalStorageWritable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }


    public static boolean isWorkImageExist(String photoName) {
        String photoPath = "IMG_" + photoName + ".png";
        return new File(getCommandPDirAllApiLevels().getAbsolutePath() + "/" + photoPath).exists();
    }


    public static void saveImage(String photoPath, Bitmap bitmap){
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(photoPath);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
            out.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String storageWorkPhoto(String photoName, Bitmap bitmap){
        String  fileName ="IMG_"+photoName+".png";
        String  photoPath = getCommandPDirAllApiLevels().getAbsolutePath()+"/"+fileName;
        saveImage(photoPath,bitmap);
        return photoPath;
    }
}
